package com.example.tool.util;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;

import org.json.JSONException;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigInteger;
import java.security.MessageDigest;

import static android.webkit.ConsoleMessage.MessageLevel.LOG;

/**
 * 描述:文件操作类
 *
 * @author: liangkun
 * @date: 2019/10/12 11:20
 */
public class FileUtil {
    private static FileUtil fileUtil;

    /**
     * @return
     * @description 单例
     * @author liangkun
     * @time 2019/10/12 11:22
     */
    public static FileUtil getInstance() {
        synchronized (FileUtil.class) {
            fileUtil = new FileUtil();
        }
        return fileUtil;
    }

    /**
     * @param file 文件
     * @return
     * @description 获取文件md5值
     * @author liangkun
     * @time 2019/10/12 11:23
     */
    public String getFileMD5(File file) {
        if (!file.exists() || !file.isFile()) {
            return "";
        }
        MessageDigest digest;
        FileInputStream in;
        byte buffer[] = new byte[1024 * 1024 * 5];
        int len;
        try {
            digest = MessageDigest.getInstance("MD5");
            in = new FileInputStream(file);
            while ((len = in.read(buffer)) != -1) {
                digest.update(buffer, 0, len);
            }
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        BigInteger bigInt = new BigInteger(1, digest.digest());
        return bigInt.toString(16);
    }

    /**
     * @return
     * @description 获取sdcard路径
     * @author liangkun
     * @time 2019/10/12 11:33
     */
    public String getSdcardPath() {
        File sdDir = null;
        boolean sdCardExist = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);//判断sd卡是否存在
        if (sdCardExist) {
            sdDir = Environment.getExternalStorageDirectory();//获取跟目录
            return sdDir.getAbsolutePath();
        }
        return null;
    }

    /**
     * @param filePath 路径
     * @description 创建文件夹
     * @author liangkun
     * @time 2019/10/12 11:34
     */
    public void createFileDir(String filePath) {
        File fileDir = new File(filePath);
        if (!fileDir.exists()) {
            DebugLog.d(DebugLog.KUN+" createFileDir。。filePath。"+filePath);
            fileDir.mkdirs();
        }else {
            DebugLog.d(DebugLog.KUN+" createFileDir。。存在。");
        }
    }

    /**
     * @param
     * @return
     * @description 创建文件
     * @author liangkun
     * @time 2019/10/12 11:59
     */
    public boolean createFile(String filePath) {
        boolean isCreat = false;
        File file = new File(filePath);
        if (!file.exists()) {
            try {
                isCreat = file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
                return isCreat;
            }
        }
        return isCreat;
    }

    /**
     * @param oldPath 旧路径
     * @param newPath 新路径
     * @description 复制文件到另一个文件
     * @author liangkun
     * @time 2019/10/12 11:37
     */
    public void cooyFileToTarget(String oldPath, String newPath) {
        DebugLog.d(LOG + "  cooyFileToSata-------------------- ");
        File file = new File(oldPath);
        InputStream bis = null;
        OutputStream bos = null;
        try {
            bis = new FileInputStream(file);
            File panFile = new File(newPath);
            if (!panFile.exists()) {
                DebugLog.d(LOG + "  cooyFileToSata--------创建-------------- ");
                panFile.createNewFile();
            } else {
                DebugLog.d(LOG + " 44444444444444444 ");
            }
            if (!panFile.canWrite()) {
                DebugLog.d(LOG + " 666666666666 ");
                return;
            }
            bos = new FileOutputStream(panFile);
            byte[] buffer = new byte[1024 * 1024];
            int len;
            while ((len = bis.read(buffer)) != -1) {
                bos.write(buffer, 0, len);
            }
            bos.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭文件流
            try {
                if (bis != null) {
                    bis.close();
                }
                if (bos != null) {
                    bos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * @param filePath 本地路径
     * @return
     * @description 根据文件路径读取byte[] 数组,生成64
     * @author liangkun
     * @time 2019/10/12 11:40
     */
    public static byte[] getBytesFromLocalPath(String filePath) throws IOException {
        File file = new File(filePath);
        if (!file.exists()) {
            throw new FileNotFoundException(filePath);
        } else {
            ByteArrayOutputStream bos = new ByteArrayOutputStream((int) file.length());
            BufferedInputStream in = null;

            try {
                in = new BufferedInputStream(new FileInputStream(file));
                short bufSize = 1024;
                byte[] buffer = new byte[bufSize];
                int len1;
                while (-1 != (len1 = in.read(buffer, 0, bufSize))) {
                    bos.write(buffer, 0, len1);
                }

                byte[] var7 = bos.toByteArray();
                return var7;
            } finally {
                try {
                    if (in != null) {
                        in.close();
                    }
                } catch (IOException var14) {
                    var14.printStackTrace();
                }
                bos.close();
            }
        }
    }

    /**
     * @param file  目标文件
     * @param mData 流文件
     * @description 将流文件写入目标文件夹
     * @author liangkun
     * @time 2019/10/12 11:43
     */
    public void writeByteToFile(File file, byte[] mData) throws IOException {
        BufferedInputStream bis = new BufferedInputStream(new ByteArrayInputStream(mData, 0, mData.length));
        FileOutputStream fos = new FileOutputStream(file);
        byte[] buff = new byte[8192];
        int len;
        while ((len = bis.read(buff)) != -1) {
            fos.write(buff, 0, len);
        }
        fos.flush();
        fos.close();
        bis.close();
    }

    /**
     * @param
     * @return
     * @description 文件转为流
     * @author liangkun
     * @time 2019/10/12 11:47
     */
    public ByteArrayOutputStream getStreamFromFile(File file) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(file);
            byte[] buff = new byte[8192];
            int len;
            while ((len = fileInputStream.read(buff)) != -1) {
                outputStream.write(buff, 0, len);

            }
            outputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                outputStream.close();
                if (fileInputStream != null) {
                    fileInputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return outputStream;
    }

    /**
     * 根据文件生成缩微图
     *
     * @param file
     * @return
     * @throws IOException
     * @throws JSONException
     */
    public String getThFilesWrite(File file) throws IOException {
        ByteArrayOutputStream outputStream = getStreamFromFile(file);
        byte[] fileByte = outputStream.toByteArray();
        Bitmap bitmap = BitmapFactory.decodeByteArray(fileByte, 0, fileByte.length);
        String thumbnail = ImageUtil.getInstance().bitmap2StrByBase64(bitmap);
        outputStream.close();
        return thumbnail;


    }

    /**
     * 根据文件路径读取byte[] 数组,生成64
     */
    public static byte[] readFileByBytes(String filePath) throws IOException {
        File file = new File(filePath);
        if (!file.exists()) {
            throw new FileNotFoundException(filePath);
        } else {
            ByteArrayOutputStream bos = new ByteArrayOutputStream((int) file.length());
            BufferedInputStream in = null;

            try {
                in = new BufferedInputStream(new FileInputStream(file));
                short bufSize = 1024;
                byte[] buffer = new byte[bufSize];
                int len1;
                while (-1 != (len1 = in.read(buffer, 0, bufSize))) {
                    bos.write(buffer, 0, len1);
                }

                byte[] var7 = bos.toByteArray();
                return var7;
            } finally {
                try {
                    if (in != null) {
                        in.close();
                    }
                } catch (IOException var14) {
                    var14.printStackTrace();
                }

                bos.close();
            }
        }
    }
}
